package com.android.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Button;

import com.android.R;
import com.android.activity.fragment.HomePageFragment;
import com.android.activity.fragment.MessagePageFragment;
import com.android.activity.fragment.MyPageFragment;
import com.android.activity.fragment.PublishProductFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.navigation.NavigationView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class HomePageActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{

    private BottomNavigationView navigation;
    private FragmentTransaction fragmentTransaction;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

//        EventBus.getDefault().register(this);

        navigation = findViewById(R.id.bottom_navigation);
        navigation.setLabelVisibilityMode(NavigationBarView.LABEL_VISIBILITY_LABELED);
        navigation.setSelectedItemId(R.id.item_home);

        //首次加载到home活动
        //开始碎片事务
        fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragment_container,new HomePageFragment());
        //提交事务
        fragmentTransaction.commit();

        //设置低航栏的点击事件
        navigation.setOnNavigationItemSelectedListener(this);


    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        //开始碎片事务
        fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        switch (item.getItemId()) {
            case R.id.item_message:
                fragmentTransaction.replace(R.id.fragment_container, new MessagePageFragment());
                //提交事务
                fragmentTransaction.commit();
                break;
            case R.id.item_my:
                fragmentTransaction.replace(R.id.fragment_container, new MyPageFragment());
                fragmentTransaction.commit();
                break;
            case R.id.item_shop:
                fragmentTransaction.replace(R.id.fragment_container, new PublishProductFragment());
                fragmentTransaction.commit();
                break;
            case R.id.item_home:
                fragmentTransaction.replace(R.id.fragment_container, new HomePageFragment());
                fragmentTransaction.commit();
                break;
        }
        return true;
    }


    @Override
    protected void onDestroy() {
//        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}